package com.sicheng.lc.周赛.分类.思维;

import java.util.Arrays;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/30 22:16
 */
public class 拿出最少数目的魔法豆 {
    //    https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/
    public long minimumRemoval(int[] beans) {
        Arrays.sort(beans);
        long sum = 0;
        for (int bean : beans) {
            sum += bean;
        }
        long res = Long.MAX_VALUE;
        for (int i = 0; i < beans.length; i++) {
            long x = sum - (long) beans[i] * (beans.length - i);
            res = Math.min(res, x);
        }
        return res;
    }


}
